package com.liachen.stock.ext.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.liachen.common.constant.Constant;
import com.liachen.common.util.ToolsUtil;
import com.liachen.stock.entity.StockSimple;
import com.liachen.stock.entity.StockSummaryInfo;
import com.liachen.stock.ext.XueqiuService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * TODO 功能描述
 *
 * @author chen.liang
 * @date 2020/1/10 16:46
 * @since V1.0
 */
@Service
public class XueqiuServiceImpl implements XueqiuService {
    private static final String XUE_QIU_STOCK_INFO = "https://stock.xueqiu.com/v5/stock/quote.json?symbol=%s&extend=detail";
    private static final String cookies = "cookiesu=271703841045799; device_id=14498f0243d1a0f9a879ce932bb9cee5; s=ac11yzw593; xq_is_login=1; u=1443754822; xq_a_token=08c534c6878a83d2f235f7b420858ef1a0d3437f; xqat=08c534c6878a83d2f235f7b420858ef1a0d3437f; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjE0NDM3NTQ4MjIsImlzcyI6InVjIiwiZXhwIjoxNzE4NjExMjY5LCJjdG0iOjE3MTYwMTkyNjkzOTcsImNpZCI6ImQ5ZDBuNEFadXAifQ.RQz5bB_SWehR_h3Y3K-EB--J8KCZHKzdpFNeZk2wEjjjRiMCAgsLn4ZJROHwVjZTaL8Jy4YM3f2BBzhDrgid2L9Gxqihlf9SYsME1AsBFzPwd4DEhj_7M-SRpiWUPyYssrA_smJFvgGuOgsNDIY3U8SKyo7qh87J2cJYtd8a3wWMh0H98KOg7HiM1V8H2vpqTe6dqFfIt4HKzFCq73_1fWhB6Zq3clsjNSB5SEFUG1NWfvBg_mMnT3TVF7CLMtnVROtuBsXW75YN3bFrBwCMoxgF0Twja3nnG-VNyqf8_I9-5SDz3FkJVcge0XmtJQpo8B92KBOTI40so9zPogFHEg; xq_r_token=28de58532f462410644f20e76348662511a7d5b4; bid=13c83b8c792417ebb54a7ca00a4ecdda_lwbtmm8e; Hm_lvt_1db88642e346389874251b5a1eded6e3=1716019270,1717127499; snbim_minify=true; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1717140154";
    @Override
    public StockSimple getStockTodayInfo(StockSummaryInfo stockSummaryInfo) throws IOException {
        String code = stockSummaryInfo.getCode().startsWith("6") || stockSummaryInfo.getCode().startsWith("5") ? "SH" + stockSummaryInfo.getCode() : "SZ" + stockSummaryInfo.getCode();
        Document doc = Jsoup.connect(String.format(XUE_QIU_STOCK_INFO, code)).cookies(ToolsUtil.getCookie(cookies)).ignoreContentType(true).userAgent(Constant.USER_AGENT).get();
        JSONObject result = JSONUtil.parseObj(doc.text());
        BigDecimal todayIncrease = result.getJSONObject("data").getJSONObject("quote").getBigDecimal("percent");
        StockSimple stockSimple = new StockSimple();
        stockSimple.setCode(stockSummaryInfo.getCode());
        stockSimple.setName(stockSummaryInfo.getName());
        stockSimple.setIncrease(todayIncrease);
        return stockSimple;
    }

    @Override
    public List<JSONObject> getStockFinanceInfo() throws IOException {
        Document doc = Jsoup.connect("https://stock.xueqiu.com/v5/stock/finance/cn/indicator.json?symbol=SH600519&type=all&is_detail=true&count=1&timestamp=1717141588139").cookies(ToolsUtil.getCookie(cookies)).ignoreContentType(true).userAgent(Constant.USER_AGENT).get();
        JSONObject result = JSONUtil.parseObj(doc.text());
        System.out.println(result);
        return null;
    }

    public static void main(String[] args) throws IOException {
        Document doc = Jsoup.connect("http://webapi.cninfo.com.cn/api/stock/p_stock2303?scode=SH600519").ignoreContentType(true).userAgent(Constant.USER_AGENT).get();
        JSONObject result = JSONUtil.parseObj(doc.text());
        System.out.println(result);
    }
}
